/*
 * StringUtils.java
 *
 * Created on 23 May 2006, 19:53
 *
 * Copyright (C)
 *
 * This program is free software; you can redistribute it and/or modify it under 
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation; either version 2 of the License, or (at your option) any later 
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT 
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with 
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple 
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */

package com.grubby.utils;

/**
 * String Utilities.
 * This class provides a bunch of methods to aid String manipulation.
 *
 * It was originally copied from examples provided by Jonathan Vaughan of
 * Monks/Vaughan Ltd.
 *
 * @author A. HARMEL-LAW. E: andrew.harmel.law@gmail.com
 * @author J.S. Vaughan
 * @version 0.1
 */
public final class StringUtils {
    
    /** No-Args Constructor */
    private StringUtils() {
    }
    
    /**
     * Appends provided <String> and a trailing space to the <StringBuffer>.
     * @param aStr <String> to append.
     * @param aBuff <StringBuffer> for append
     */
    public static void appendWithTrailingSpace(String aStr, StringBuffer aBuff) {
        
        if (aStr != null) {
            aBuff.append(aStr);
            aBuff.append(" ");
        }
        
    }
    
    /**
     * Null-safe implementation of <StringBuffer>.append.  Only performs the
     * action if the provided <String> is not null.
     * @param aStr <String> to append
     * @param aBuff <StringBuffer> for append
     */
    public static void nullsafeAppend(String aStr, StringBuffer aBuff) {
        
        if (aStr != null) {
            aBuff.append(aStr);
        }
        
    }
    
    /**
     * Null-safe appender of a <String>, a comma and a space to a provided 
     * <StringBuffer>.  Only performs the action if the provided String is not 
     * null.
     * @param aStr <String> to append
     * @param aBuff <StringBuffer> for append
     */
    public static void nullsafeAppendCommaSpace(String aStr,
            StringBuffer aBuff) {
        
        if (aStr != null) {
            aBuff.append(aStr);
            aBuff.append(", ");
        }
        
    }
    
    /**
     * Null-safe appender of a <String>, and a comma to a provided 
     * <StringBuffer>.  Only performs the action if the provided String is not 
     * null.
     * @param aStr String to append
     * @param aBuff String buffer for append
     */
    public static void nullsafeAppendComma(String aStr, StringBuffer aBuff) {
        
        if (aStr != null) {
            aBuff.append(aStr);
            aBuff.append(", ");
        }
        
    }
    
    /**
     * Tests to see if the provided <String> is NULL or of zero length.
     * @param The <String> to test
     * @return 'true' if the Strign is null or of zero length.
     */
    public static boolean isBlank(String testMe) {
        if (testMe == null || testMe.length() == 0)
            return true;
        else
            return false;
    }
    
    /**
     * Pad a <String> up to a certain size with a char on the left ('true') or 
     * on the right ('false')
     * @param aString The <String> to be padded
     * @param aLength Final intended length for the final <String>
     * @param aChar The pad char
     * @param paddingLeft 'true' - pad from left, 'false' - pad from right
     * @return The padded <String>
     */
    public static String paddingString(String aString, int aSize, char aChar, 
            boolean paddingLeft) {
        
        StringBuffer str = new StringBuffer(aString);
        int strLength = str.length();
        if (aSize > 0 && aSize > strLength) {
            for (int i = 0; i <= aSize; i++) {
                if (paddingLeft) {
                    if (i < aSize - strLength) {
                        str.insert(0, aChar);
                    }
                } else {
                    if (i > strLength) {
                        str.append(aChar);
                    }
                }
            }
        }
        
        return str.toString();
        
    }
    
} // end of class StringUtils.java